/**
 * chinagoods.com
 * Copyright (c) 2020-2024 All Rights Reserved.
 */
package com.ruoyi.system.biz.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.enums.OrderStatusEnum;
import com.ruoyi.system.biz.UserCountBizService;
import com.ruoyi.system.domain.CashHistory;
import com.ruoyi.system.domain.Order;
import com.ruoyi.system.domain.UserCount;
import com.ruoyi.system.domain.Withdraw;
import com.ruoyi.system.service.CashHistoryService;
import com.ruoyi.system.service.OrderService;
import com.ruoyi.system.service.UserCountService;
import com.ruoyi.system.service.WithdrawService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author yangpeng
 * Created by on 2024-01-30 13:04
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class UserCountBizServiceImpl extends BaseBizServiceImpl implements UserCountBizService {

    private final UserCountService userCountService;
    private final WithdrawService withdrawService;
    private final OrderService orderService;
    private final CashHistoryService cashHistoryService;

    @Override
    public void addUserCount() {
        DateTime end = DateUtil.beginOfDay(new Date());
        DateTime start = DateUtil.offsetDay(end, -1);
        addUserCount(start, end);
    }

    @Override
    public void addUserCountForMonth() {
        DateTime start = DateUtil.beginOfMonth(new Date());
        DateTime end = DateUtil.offsetDay(start, 1);
        DateTime dateTime = DateUtil.beginOfDay(new Date());
        while (true) {
            if (start.getTime() == dateTime.getTime()) {
                log.info("现在:{}", start);
                break;
            }
            addUserCount(start, end);
            start = DateUtil.offsetDay(start, 1);
            end = DateUtil.offsetDay(end, 1);
        }

    }

    //统计昨天的数据
    @Override
    public void addUserCount(Date start, Date end) {
        List<Long> userIds = orderService.selectUserIds(start, end);
        if (CollectionUtil.isEmpty(userIds)) {
            return;
        }
        for (int i = 0; i < userIds.size(); i++) {
            BigDecimal earning = BigDecimal.ZERO;
            Long userId = userIds.get(i);
            Order order = new Order();
            order.setUserId(userId);
            order.setOrderStatus(OrderStatusEnum.success.getValue());
            PageDomain pageDomain = new PageDomain();
            pageDomain.setPageSize(1);
            pageDomain.setBeginTime(DateUtil.formatDateTime(start));
            pageDomain.setEndTime(DateUtil.formatDateTime(end));

            CashHistory cashHistory = new CashHistory();
            cashHistory.setUserId(userId);
            List<CashHistory> list = cashHistoryService.findList(cashHistory, pageDomain);
            if (CollectionUtil.isNotEmpty(list)) {
                earning = list.get(0).getEarning();
            }
            //统计成功订单数
            int orderNum = orderService.countByDomain(order, start, end, "sendTime");
            //统计订单金额
            BigDecimal orderAmount = orderService.sum("payAmount", order, start, end, "sendTime");
            //统计充值金额
            BigDecimal rechargeAmount = this.getRecharge(userId, start, end);
            //组装数据并插入
            UserCount userCount = new UserCount();
            userCount.setUserId(userId);
            userCount.setFinishAmount(orderAmount);
            userCount.setFinishNum(orderNum);
            userCount.setRecharge(rechargeAmount);
            userCount.setCountDate(DateUtil.formatDate(start));
            userCount.setEarning(earning);
            userCountService.addDomain(userCount);
        }

    }

    //统计充值金额
    private BigDecimal getRecharge(Long userId, Date start, Date end) {
        Withdraw withdraw = new Withdraw();
        String type = "recharge";
        withdraw.setType(type);
        withdraw.setUserId(userId);
        withdraw.setStatus("pass");
        BigDecimal todayAmount = withdrawService.sum("money", withdraw, start, end);
        return todayAmount;
    }


}